/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cinema.client.cinemaclient.ui;

import com.cinema.client.cinemaclient.managers.UserManager;
import com.cinema.client.cinemaclient.model.User;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author Zsolt Szecsi
 */
public class LoginFrame extends JFrame {

    private JTextField userNameTxt;
    private JPasswordField passwordTxt;
    private JButton loginBtn, registerBtn;
    private UserManager userManager;

    public LoginFrame() {
        super("Login");
        // init simple attributes
        userManager = new UserManager();
        // init elements
        JPanel mainPanel = new JPanel();
        JLabel usernameLbl = new JLabel("Username: ");
        JLabel passwordLbl = new JLabel("Password: ");
        userNameTxt = new JTextField();
        passwordTxt = new JPasswordField();
        loginBtn = new JButton("Login");
        registerBtn = new JButton("Register");

        // compose ui
        mainPanel.setLayout(new GridLayout(3, 2));

        mainPanel.add(usernameLbl);
        mainPanel.add(userNameTxt);
        mainPanel.add(passwordLbl);
        mainPanel.add(passwordTxt);
        mainPanel.add(loginBtn);
        mainPanel.add(registerBtn);

        this.setContentPane(mainPanel);
        this.setBounds(100, 100, 300, 100);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setListener();
    }

    private void setListener() {
        registerBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                RegisterFrame registerFrame = new RegisterFrame(LoginFrame.this);
                registerFrame.setVisible(true);
                LoginFrame.this.setVisible(false);
            }
        });
        loginBtn.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                if ("".equals(userNameTxt.getText()) && "".equals(passwordTxt.getText())) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "Error: Empty field!");
                    return;
                }
                User user = userManager.getUserByUsername(userNameTxt.getText());
                if (user == null) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "Error: Incorrect username or password!");
                } else {
                    MovieFrame movieFrame = new MovieFrame(user);
                    movieFrame.setVisible(true);
                    LoginFrame.this.dispose();
                }
            }
        });
    }
}
